package cn.openea.perpartner.payment.web.points.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.utils.Arith;
import cn.openea.perpartner.common.core.utils.StringUtils;
import cn.openea.perpartner.payment.web.points.dto.FreezeDto;
import cn.openea.perpartner.payment.web.points.dto.PointsDto;
import cn.openea.perpartner.payment.web.points.entity.Freeze;
import cn.openea.perpartner.payment.web.points.mapper.FreezeMapper;
import cn.openea.perpartner.payment.web.points.service.IFreezeService;
import cn.openea.perpartner.payment.web.points.service.IPointsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author Weird077
 * @description TODO
 * @date 2022-02-08 22:15
 */
@Service
public class FreezeServiceImpl extends ServiceImpl<FreezeMapper, Freeze> implements IFreezeService {
    @Resource
    private IPointsService pointsService;

    @Override
    public boolean freeze(FreezeDto dto) {
        Freeze freeze = new Freeze();
        BeanUtil.copyProperties(dto, freeze, true);
        freeze.setRealPoints(dto.getPoints());
        freeze.setStatus("1");
        return save(freeze);
    }

    @Override
    public boolean closure(FreezeDto freezeDto) {
        LambdaQueryWrapper<Freeze> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(freezeDto.getFreezeOrderId())) {
            wrapper.eq(Freeze::getOrderId, freezeDto.getFreezeOrderId());
        }
        if (StringUtils.isNotEmpty(freezeDto.getSourceId())) {
            wrapper.eq(Freeze::getSourceId, freezeDto.getSourceId());
        }
        Freeze freeze = getOne(wrapper);
        freeze.setStatus("0");
        return updateById(freeze);
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public boolean settlement(FreezeDto freezeDto) {
        PointsDto pointsDto = new PointsDto();
        BeanUtil.copyProperties(freezeDto, pointsDto, true);
        pointsDto.setOperType("freeze");
        //减少用户积分
        pointsDto.setUserId(freezeDto.getUserId());
        boolean isSuccess = pointsService.settlement(pointsDto);
        if (isSuccess) {
            //给拍档增加积分
            pointsDto.setSourceId(freezeDto.getFreezeOrderId());
            pointsDto.setUserId(freezeDto.getTargetUserId());
            pointsService.add(pointsDto);
            //更新冻结积分
            LambdaQueryWrapper<Freeze> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Freeze::getOrderId, freezeDto.getFreezeOrderId());
            Freeze freeze = getOne(wrapper);
            double freezePoints = Convert.toDouble(freeze.getRealPoints());
            double realPoints = Arith.sub(freezePoints, Convert.toDouble(freezeDto.getPoints()));
            freeze.setRealPoints(Convert.toStr((int) realPoints));
            return updateById(freeze);
        }
        return false;
    }

    @Override
    public List<Freeze> selectList(FreezeDto freezeDto) {
        LambdaQueryWrapper<Freeze> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Freeze::getStatus, freezeDto.getStatus());
        if (StrUtil.isNotBlank(freezeDto.getUserId())) {
            wrapper.eq(Freeze::getUserId, freezeDto.getUserId());
        }
        if (StrUtil.isNotBlank(freezeDto.getAuthUserId())) {
            wrapper.in(Freeze::getAuthUserId, Arrays.asList(freezeDto.getAuthUserId().split(",")));
        }
        if (StrUtil.isNotBlank(freezeDto.getSceneId())) {
            wrapper.in(Freeze::getSceneId, Arrays.asList(freezeDto.getSceneId().split(",")));
        }
        if (StrUtil.isNotBlank(freezeDto.getStartTime()) && StrUtil.isNotBlank(freezeDto.getEndTime())) {
            wrapper.between(Freeze::getCreateTime, freezeDto.getStartTime(), freezeDto.getEndTime());
        }
        wrapper.orderByDesc(Freeze::getCreateTime);
        return baseMapper.selectList(wrapper);
    }
    @Override
    public List<Freeze> selectList(PointsDto pointsDto) {
        LambdaQueryWrapper<Freeze> wrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(pointsDto.getUserId())){
            wrapper.eq(Freeze::getUserId, pointsDto.getUserId());
        }
        if (StrUtil.isNotBlank(pointsDto.getSceneId())) {
            wrapper.eq(Freeze::getSceneId, pointsDto.getSceneId());
        }
        if (StrUtil.isNotBlank(pointsDto.getStatus())){
            wrapper.eq(Freeze::getStatus, pointsDto.getStatus());
        }
        wrapper.orderByDesc(Freeze::getCreateTime);
        return list(wrapper);
    }

}
